package com.ship.controller;

import com.ship.core.annotation.RequestDecryptBody;
import com.ship.core.annotation.ResponseEncryptBody;
import com.ship.entity.bo.Users;
import com.ship.entity.dto.*;
import com.ship.service.AdvertService;
import com.ship.service.UsersService;
import com.ship.service.VideoService;
import com.ship.util.ValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private VideoService videoService;
    @Autowired
    private UsersService usersService;


    @PostAuthorize("hasRole('USER')")
    @RequestMapping(value = "/getUserId", method = RequestMethod.POST)
    @ResponseEncryptBody
    public JsonResult getUser(@RequestDecryptBody JsonRequest<UserInfoRequest> requestJsonRequest) {
        Users u = usersService.queryById(requestJsonRequest.getParameters().getUserId());
        UserInfoResponse infoResponse = new UserInfoResponse();
        infoResponse.setUsername(u.getUsername());
        infoResponse.setUserId(u.getId());
        infoResponse.setSeqNo(u.getSeqNo());
        return new JsonResult(ResultCode.SUCCESS,"获取数据成功",infoResponse);
    }
    @PostAuthorize("hasRole('USER')")
    @RequestMapping(value = "/editPwd", method = RequestMethod.POST)
    @ResponseEncryptBody
    public JsonResult editPwd(@RequestDecryptBody JsonRequest<EditPwdRequest> requestJsonRequest){
        int falg = usersService.editPwd(requestJsonRequest.getParameters().getUserId(),requestJsonRequest.getParameters().getOldPwd(),requestJsonRequest.getParameters().getNewPwd());
        if(falg==0){
            return new JsonResult(ResultCode.SUCCESS,"修改密码成功");
        }else if (falg==1){
            return new JsonResult(ResultCode.SYS_ERROR,"用户不存在");
        }else if (falg==2){
            return new JsonResult(ResultCode.SYS_ERROR,"旧密码错误");
        }else {
            return new JsonResult(ResultCode.SYS_ERROR,"未知错误");
        }
    }


    /**
     * 获取视频内容
     */
    @RequestMapping(value = "getVideoContent",method = RequestMethod.POST)
    @ResponseEncryptBody
    public JsonResult getVideoContent(@RequestDecryptBody JsonRequest<VideoContentDto> videoContentDto){
        logger.info("[获取m3u8请求信息]{}",videoContentDto);
        JsonResult json = ValidationUtils.validateEntity(videoContentDto.getParameters());
        if(json==null){
            Users users = usersService.queryById(videoContentDto.getParameters().getUserId());
            if(users.getVipTime().before(new Date())){
                JsonResult jsonResult = new JsonResult(ResultCode.SYSTEM_ERROR, "会员失效，观看请充值!");
                return jsonResult;
            }
            try {
                Map<String,Object> content = videoService.getVideo(videoContentDto.getParameters().getVideoUrl(),videoContentDto.getParameters().getVideoType());
                if(content==null){
                    JsonResult jsonResult = new JsonResult(ResultCode.SYSTEM_ERROR, "加载失败");
                    logger.info("[获取视频内容]{}", jsonResult);
                    return jsonResult;
                }else {
                    JsonResult jsonResult = new JsonResult(ResultCode.SUCCESS, "成功", content);
                    logger.info("[获取视频内容]{}", jsonResult);
                    return jsonResult;
                }
            } catch (Exception e) {
                e.printStackTrace();
                JsonResult jsonResult = new JsonResult(ResultCode.SYSTEM_ERROR, "加载失败");
                logger.info("[获取视频内容]{}", jsonResult);
                return jsonResult;
            }
        }
        return json;
    }
}
